package psend

import (
	"bytes"
	"encoding/gob"
	"fmt"
	"net"
)

func encode_gob(data any) ([]byte, error) {
	var buf bytes.Buffer
	enc := gob.NewEncoder(&buf)
	err := enc.Encode(data)
	if err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}
func decode_gob(data []byte, out any) error {
	dec := gob.NewDecoder(bytes.NewReader(data))
	return dec.Decode(out)
}

// server端的处理函数
func han(handler func(c *C_content), spath string)   {
	
	listener, err := net.Listen("unix", spath)
	if err != nil {
		fmt.Println("listen error:", err)
		listener.Close()
		panic(err)
	}
	fmt.Println(spath+"挂载完成")
	defer listener.Close()
	for {
		conn, err := listener.Accept()
		if err != nil {
			fmt.Println("accept error:", err)
			listener.Close()
			break;
		}
		fmt.Println("新连接:", conn.RemoteAddr())
		c := C_content{spath, conn}
		go handler(&c)
	}
}
